package com.itcat.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.PmsProductCategoryApi;
import com.itcat.common.CommonPage;
import com.itcat.mysql.param.PageParam;
import com.itcat.mysql.pojo.PmsProductCategory;
import com.itcat.mysql.pojo.PmsProductCategoryDto;
import com.itcat.mysql.vo.PmsProductCategoryVo;
import com.itcat.service.PmsProductCategoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jerry Broker
 * @project_name rainbow-mall
 * @create 2022-04-02 18:51
 */
@Service
public class PmsProductCategoryServiceImpl implements PmsProductCategoryService {

    @DubboReference
    private PmsProductCategoryApi pmsProductCategoryApi;


    /**
     * 查询所有一级分类及子分类
     *
     * @return
     */
    @Override
    public List<PmsProductCategoryVo> withChildren() {

        // 查询一级分类
        List<PmsProductCategoryVo> voList = getChildren(0L);

        return voList;
    }


    /**
     * 获取分类层级数据
     * @param id
     * @return
     */
    public List<PmsProductCategoryVo> getChildren(Long id) {

        // 构建结果集
        ArrayList<PmsProductCategoryVo> categoryVoArrayList = new ArrayList<>();

        List<PmsProductCategory> categoryList = pmsProductCategoryApi.getProductCategory(id);
        if (categoryList.isEmpty()) {
            return null;
        }

        for (PmsProductCategory category : categoryList) {
            List<PmsProductCategoryVo> children = getChildren(category.getId());
            PmsProductCategoryVo init = PmsProductCategoryVo.init(category, children);
            categoryVoArrayList.add(init);
        }

        return categoryVoArrayList;
    }


    /**
     * 通过parentId查询分类信息
     * @param parentId
     * @param pageParam
     * @return
     */
    @Override
    public CommonPage<PmsProductCategory> listByParentId(Integer parentId, PageParam pageParam) {
        Page<PmsProductCategory> page = pmsProductCategoryApi.listByParentId(parentId, pageParam);
        return CommonPage.restPage(page);
    }

    @Override
    public void create(PmsProductCategoryDto dto) {
        pmsProductCategoryApi.create(dto);
    }



    @Override
    public void updateShowStatus(Integer ids, Integer navStatus) {
        pmsProductCategoryApi.updateShowStatus(ids,navStatus);
    }

    @Override
    public void updateNavStatus(Integer ids, Integer navStatus) {
        pmsProductCategoryApi.updateNavStatus(ids,navStatus);
    }

    @Override
    public void deleteById(Integer parentId) {
        pmsProductCategoryApi.deleteById(parentId);
    }

    @Override
    public PmsProductCategory selectById(Integer parentId) {
        PmsProductCategory pmsProductCategory = pmsProductCategoryApi.selectById(parentId);
        return pmsProductCategory;
    }
}
